package part17;

import java.util.*;

/**
 * @author wcj
 * @Date 2021/6/1 17:55
 * @Version 1.0
 */
public class Part176 {

    /**
     * 可选操作，最典型的是：
     * Arrays.asList():这个方法返回的是一个固定大小的list,即调用对list大小进行修改的操作都会抛出一个UnsupportedOperationException异常
     * Collections.unmodifiedList()会返回一个不可修改的list
     *
     * @param msg
     * @param list
     */
    public static void test(String msg, List<String> list) {
        System.out.println("=========" + msg + "==========");
        Collection<String> c = list;
        List<String> subList = list.subList(1, 8);
        ArrayList<String> c2 = new ArrayList<>(subList);
        try {
            c.retainAll(c2);
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            c.removeAll(c2);
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            c.clear();
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            c.add("X");
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            c.addAll(c2);
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            c.remove("C");
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            list.set(0, "X");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("A B C D E F G H J K L M N".split(" "));
        test("test copy", new ArrayList<>(list));
        test("test list", list);
        test("unmodifiableList", Collections.unmodifiableList(new ArrayList<>(list)));
    }
}
